#! /usr/bin/python
# -*- coding: utf-8 -*-

import grpc
import os
import sys
import time

from api import *

class grpcClient():
    connect_serv = False
    
    def __init__(self):
        self.connect()        

    def connect(self):

        self.channel = grpc.insecure_channel('localhost:50051')
        self.stub = ProcDataServ_pb2_grpc.RealTimeProcStub(self.channel)
        self.connect_serv = True
        

    def GetPlateSetData(self):
        # print("connect: ", self.connect_serv)        
        if not self.connect_serv:
            self.connect()
            print("grpcClient::GetPlateSetData")

        try:
            if self.connect_serv:
                #[todo]
                ret = self.stub.GetPlateSetData(ProcDataServ_pb2.PlateRequest())
                # if ret and len(ret.set):
                #     print("recv plate[0]: {},  plate2: {}".format(ret.set[0].plate_id,ret.set[1].plate_id))
                # else:
                #     print("recv plate set none")
                return ret
            else:
                return None
        except Exception as e:
            print("getplateset error: ", e)
            self.connect_serv = False
            return None
            

    def GetPlateActData(self,tm_from,tm_to):
        if not self.connect_serv:
            self.connect()
        
        try:
            if self.connect_serv:
                #[todo]
                print("start GetPlateActData")
                ret = self.stub.GetPlateActData(ProcDataServ_pb2.PlateRequest(tm_from=tm_from, tm_to=tm_to))
                return ret
            else:
                return None
        except Exception as e:
            self.connect_serv = False
            return None

